package com.gomore.opple.module.searchgoods;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gomore.opple.R;
import com.gomore.opple.model.EventKeySearch;
import com.gomore.opple.module.BaseFragment;
import com.gomore.opple.module.IntentStart;
import com.gomore.opple.module.searchgoods.adapter.FilterHotTagAdapter;
import com.gomore.opple.module.searchgoods.adapter.HistoryAdapter;
import com.gomore.opple.utils.DialogUtils;
import com.gomore.opple.widgets.MyGridView;

import butterknife.Bind;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;


/**
 * Created by robot
 */
public class SearchGoodsFragment extends BaseFragment implements SearchGoodsContract.View {

    @Bind(R.id.back)
    LinearLayout back;
    @Bind(R.id.search_edittext)
    EditText search_edittext;
    @Bind(R.id.search)
    TextView search;
    @Bind(R.id.delete)
    ImageView delete;
    @Bind(R.id.my_grid_view_history)
    MyGridView my_grid_view_history;
    @Bind(R.id.my_grid_view_tag)
    MyGridView my_grid_view_tag;
    private FilterHotTagAdapter filterHotTagAdapter;
    private HistoryAdapter historyAdapter;
    private String keyWord;


    private SearchGoodsContract.Presenter mPresenter;
    private String key;

    public static SearchGoodsFragment getInstance() {
        return new SearchGoodsFragment();
    }

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_search_goods;
    }

    @Override
    protected void initalizeData() {
        if (getArguments() != null) {
            key = getArguments().getString(IntentStart.SEARCHKEY);
        }
        if (!TextUtils.isEmpty(key)) {
            search_edittext.setText(key);
            search_edittext.setSelection(key.length());
        }
    }

    @Override
    protected void initalizeViews() {
        //展示历史数据
        historyAdapter = new HistoryAdapter(getActivity(), mPresenter.getHistoryData());
        my_grid_view_history.setAdapter(historyAdapter);
        my_grid_view_history.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                keyWord = mPresenter.getHistoryData().get(position);
                search_edittext.setText(keyWord);
            }
        });
        //展示热门搜索
        filterHotTagAdapter = new FilterHotTagAdapter(getActivity(), mPresenter.getHotTagData());
        my_grid_view_tag.setAdapter(filterHotTagAdapter);
        my_grid_view_tag.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                keyWord = mPresenter.getHotTagData().get(position).getTypeName();
                search_edittext.setText(keyWord);
            }
        });
        mPresenter.prepareInitData();
        mPresenter.getHotTag();

        search_edittext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String str = s.toString().trim();
                if (!TextUtils.isEmpty(str)) {
                    search_edittext.setSelection(str.length());
                }
            }
        });

    }


    @Override
    public void setPresenter(SearchGoodsContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showProgressDialog() {
        DialogUtils.showLoadingDialog(getActivity(), getResources().getString(R.string.loading));
    }

    @Override
    public void hideProgressDialog() {
        DialogUtils.closeLoadingDialog();
    }

    @Override
    public void showMessage(String msg) {
        showToast(msg);
    }

    @Override
    public void showMessage(int msgId) {
        showToast(msgId);
    }

    @Override
    public void showHistoryFilter() {
        historyAdapter.notifyDataSetChanged();
    }

    @Override
    public void showHotTag() {
        filterHotTagAdapter.notifyDataSetChanged();
    }

    @OnClick({R.id.back, R.id.search, R.id.delete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                getActivity().finish();
                break;
            case R.id.delete:
                mPresenter.clearHistory();
                break;
            case R.id.search:
                String key = search_edittext.getText().toString().trim();
                if (!TextUtils.isEmpty(key)) {
                    //先保存到本地
                    mPresenter.saveSearchKey(key);
                }
                //传递到搜索界面
                EventKeySearch eventKeySearch = new EventKeySearch();
                eventKeySearch.setKey(search_edittext.getText().toString().trim());
                EventBus.getDefault().post(eventKeySearch);
                getActivity().finish();
                break;
        }
    }
}
